package com.wdhcr.controller.admin;

import com.wdhcr.entity.Tag;
import com.wdhcr.enums.ResultCode;
import com.wdhcr.service.TagService;
import com.wdhcr.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

/*
 * @description: 标签信息的contrller层
 * @author: wdhcr
 * @dateTime: 2020/3/19 21:07
 */
@RestController
public class TagController {

    @Autowired
    private TagService tagService;

    @CrossOrigin
    @PostMapping("/saveTag")
    public ResponseResult saveTag(@RequestBody Tag tag) {
        if (tag.getName() == null || tag.getName() == "") {
            return new ResponseResult(ResultCode.NOT_FOUND.getCode(), "分类信息不能为空！", null);
        }
        Tag tagByName = tagService.getTagByName(tag.getName());
        if (tagByName != null) {
            return new ResponseResult(ResultCode.INTERNAL_SERVER_ERROR.getCode(), "该分类已经存在！", null);
        }
        Tag tag1 = tagService.saveTag(tag);
        if (tag1 != null) {

                return new ResponseResult(ResultCode.SUCCESS.getCode(), "添加分类成功！", tag1);

        }
        return new ResponseResult(ResultCode.INTERNAL_SERVER_ERROR.getCode(), "保存分类信息失败！", null);
    }

    /*
     * @description: 根据id查询分类信息的接口
     * @params: [id]
     * @return: com.wdhcr.utils.ResponseResult
     * @author: wdhcr
     * @dateTime: 2020/3/23 21:42
     */
    @CrossOrigin
    @GetMapping("getTagById/{id}")
    public ResponseResult getTagById(@PathVariable Long id){
        Tag tag = tagService.getTag(id);
        if (tag != null){
            return new ResponseResult(ResultCode.SUCCESS.getCode(),"获取成功！",tag);
        }
        return new ResponseResult(ResultCode.FAIL.getCode(),"获取失败！",null);
    }

    /*
     * @description:根据id删除对应分类
     * @params: [id]
     * @return: com.wdhcr.utils.ResponseResult
     * @author: wdhcr
     * @dateTime: 2020/3/23 21:46
     */
    @CrossOrigin
    @GetMapping("deleteTagById/{id}")
    public ResponseResult deleteTagById(@PathVariable Long id) throws Exception {
        try {
            tagService.delete(id);

            return new ResponseResult(ResultCode.SUCCESS.getCode(),"删除成功！",null);

        }catch (Exception e){
            throw  new Exception("删除失败,当前标签有关联博客！");
        }

    }

    /*
     * @description: 前端分页查询的条数的接口
     * @params: [page, size]
     * @return: com.wdhcr.utils.ResponseResult
     * @author: wdhcr
     * @dateTime: 2020/3/21 21:14
     */
    @CrossOrigin
    @GetMapping("getTagList/{page}/{size}")
    public ResponseResult getTagList(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        //根据更新时间倒叙排列
        Sort sort = Sort.by(Sort.Direction.DESC,"id");
        PageRequest pageRequest = PageRequest.of(page, size,sort);
        Page<Tag> all = tagService.listTag(pageRequest);
        if (!all.isEmpty()){
            return new ResponseResult(ResultCode.SUCCESS.getCode(),"获取列表成功！",all);
        }
        return new ResponseResult(ResultCode.SUCCESS.getCode(),"暂无标签信息！",all);

    }



}
